

food.controller('CompanyListConroller',function($http,$scope,page){

 // 查询公司表的总数

 $http({

 	method:'get',
 	url:'../Api/admin.php?action=company_count'

 }).then(function(res){

   $scope.totalItems=res.data.c;

 });

 // 组装分页
    $scope.currentPage = 1;
    $scope.bigTotalItems = 8;
    $scope.maxSize = 5;

    var url = "../Api/admin.php?action=company_list";
    
    // 默认第一页的页码  数据默认第一页显示
    page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(result){
        $scope.company_list = result;

        console.log($scope.company_list);
    });
    
    // 页码改变时 数据根据页码改变而改变
    $scope.pageChanged = function(){
        page.getData(url,$scope.currentPage,$scope.bigTotalItems).then(function(result){
            $scope.company_list = result;
        });
    };
   
});

food.controller('CompanyAddConroller',function($scope,$http,$location,Upload){

 //上传文件
    $scope.uploadImg = function(){

        if($scope.company.company_img){
            console.log($scope.company.company_img);
            Upload.upload({
                url:"../Api/admin.php?action=company_add",

                file:$scope.company.company_img

            }).success(function(result){

                $scope.company.company_img = "uploads/"+result.company_img;
            });
        }
    };

    //提交表单  点击添加表单的方法 post提交
    $scope.saveForm = function(){

        $http({
            method:"POST",
            url:"../Api/admin.php?action=company_add",
            data:$scope.company,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }


        }).then(function(result){
            if(result.data){
                $location.path('company_list');
            }else{
                alert('添加失败');
                $location.path('company_add');
            }
        });
    }
});

// $scope.food.food_time = time.timestampToTime($scope.food.food_time);
food.controller('CompanyEditeConroller',function($scope,$http,$location,$stateParams,time,Upload){
   
  $http({
   
   method:'get',
   url:'../Api/admin.php?action=company_detail&company_id='+$stateParams.company_id

  }).then(function(res){

   $scope.company=res.data;
   $scope.company.company_time = time.timestampToTime($scope.company.company_time);
   console.log($scope.company);
  });

  // 修改信息
  
 //上传文件
    $scope.uploadImg = function(){

        if($scope.company.company_img){
            console.log($scope.company.company_img);
            Upload.upload({
                url:"../Api/admin.php?action=company_edite",

                file:$scope.company.company_img

            }).success(function(result){

                $scope.company.company_img = "uploads/"+result.company_img;
            });
        }
    };

        $scope.saveForm = function(){

        $http({
            method:"POST",
            url:"../Api/admin.php?action=company_edite&company_id="+$stateParams.company_id,
            data:$scope.company,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest:function(obj){
                var str = [];
                for(var s in obj){

                    str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));
                }

                return str.join("&");
            }


        }).then(function(result){

            if(result.data){
                $location.path('company_list');
            }else{
                alert('添加失败');
                $location.path('company_add');
            }

        });
    }

});

food.controller('CompanyDeleteConroller',function($http,$stateParams,$location){
 

 $http({
  
  method:'get',

  url:'../Api/admin.php?action=company_delete&company_id='+$stateParams.company_id


 }).then(function(res){
      


 	if(res.data){

 		$location.path("company_list");

 	}else{

        alert("删除失败");
        $location.path("company_list");
 	}
 })



})
